package com.ygxsk.carhome.ui.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.park.keyboard.InputViewSelectKeyboard;
import com.park.keyboard.OnInputChangedListener;
import com.park.keyboard.PopupKeyboard;
import com.park.keyboard.view.InputView;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.enumwrap.CarLicenseNumberType;
import com.ygxsk.carhome.listener.MyArchiveFileCarInfoAddListener;
import com.ygxsk.carhome.listener.MyKeyboardListener;
import com.ygxsk.carhome.response.FileCarInfoAddSubmit;
import com.ygxsk.carhome.views.ClearEditTextTouch;

import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-11-07  09:03
 * @Description:多车辆
 */
public class ArchiveFileCarInfoAddSubmitAdapter extends BaseQuickAdapter<FileCarInfoAddSubmit, BaseViewHolder> {

    //该界面的activity
    private Activity mActivity;

    //添加单个车辆
    private MyArchiveFileCarInfoAddListener myArchiveFileCarInfoAddListener;

    public ArchiveFileCarInfoAddSubmitAdapter(int layoutResId, @Nullable List<FileCarInfoAddSubmit> data, Activity _activity, MyArchiveFileCarInfoAddListener _myArchiveFileCarInfoAddListener) {
        super(layoutResId, data);
        mActivity = _activity;
        this.myArchiveFileCarInfoAddListener = _myArchiveFileCarInfoAddListener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, FileCarInfoAddSubmit _fileCarInfoAddSubmit) {

        if (_fileCarInfoAddSubmit != null) {

            //一个单独的id 可能有值，可能没有值；
//            int id = _fileCarInfoAddSubmit.getId();
            //编号
            int identifierNumber = _fileCarInfoAddSubmit.getIdentifierNumber();

            //车辆品牌的id
            String carBranchId = _fileCarInfoAddSubmit.getCarBranchId();
            //车辆品牌的名称
            String carBranchName = _fileCarInfoAddSubmit.getCarBranchName();

            //车系的id
            String carSeriesId = _fileCarInfoAddSubmit.getCarSeriesId();
            //车系的名称
            String carSeriesName = _fileCarInfoAddSubmit.getCarSeriesName();

            //车型 是文本框 输入的
            String carModelName = _fileCarInfoAddSubmit.getCarModelName();
            //车架好
            String carVin = _fileCarInfoAddSubmit.getVin();

            //车牌号
            String licenseNumber = _fileCarInfoAddSubmit.getLicenseNumber();
            //车辆的发动机号
            String engineNum = _fileCarInfoAddSubmit.getEngineNum();


            //车牌的外侧
            LinearLayout linearLayoutCarBrandNameOut = (LinearLayout) baseViewHolder.getView(R.id.linearlayoutcarbrandnameoutcar);

            //右侧的删除按钮；
            ImageView imageViewDeleteInFileCarInfo = (ImageView) baseViewHolder.getView(R.id.imageviewdeleteinmfilecarinfo);

            //车辆品牌 是选择结构
            TextView textVieweBrandModel = (TextView) baseViewHolder.getView(R.id.textviewebrandmodel);
            //车辆品牌默认填充第一条数据：2024-2-19 17:07:38

            //车系的外侧点击结果；
            LinearLayout layoutCarModelSecondSide = (LinearLayout) baseViewHolder.getView(R.id.layoutcarmodelsecondsidecar);
            //车辆车系
            TextView textViewCarModelSecond = (TextView) baseViewHolder.getView(R.id.textviewcarmodelsecondcar);

            //车辆车型 手动输入
            ClearEditTextTouch editViewInputEvaluateCarModel = (ClearEditTextTouch) baseViewHolder.getView(R.id.editviewinputevaluatecarmodelcar);
            //车辆车架号
            ClearEditTextTouch editViewCarFrame = (ClearEditTextTouch) baseViewHolder.getView(R.id.editviewcarframecar);

            //是普通车或者是新能源车
            RadioGroup radioGroupCarType_carItem = (RadioGroup) baseViewHolder.getView(R.id.radiogroupcartype_caritem);
            RadioButton radioButtonCarNomal_carItem = (RadioButton) baseViewHolder.getView(R.id.radiobuttoncarnomal_caritem);
            RadioButton radioButtonCarNewEnergy_carItem = (RadioButton) baseViewHolder.getView(R.id.radiobuttoncarnewenergy_caritem);

            radioButtonCarNomal_carItem.setChecked(true);
            radioButtonCarNomal_carItem.setTextColor(ColorUtils.getColor(R.color.headmanblue));

            radioButtonCarNewEnergy_carItem.setChecked(false);
            radioButtonCarNewEnergy_carItem.setTextColor(ColorUtils.getColor(R.color.dark));


            //车辆车牌号
//          ClearEditTextTouch editViewCarCard = (ClearEditTextTouch) baseViewHolder.getView(R.id.editviewcarcardcar);
            InputView mInputViewEditCarItem = (InputView) baseViewHolder.getView(R.id.input_view_caritem);
            PopupKeyboard mPopupKeyboard = new PopupKeyboard(mActivity, new MyKeyboardListener() {
                @Override
                public void doSomeThing() {

                    //因为车牌不是必填项
                    //检验车牌的正确性
                    //如果填写了车牌，而恰恰又不是正确的车牌，那么需要校验下：
//                    method_check_licenseNumber();

                }
            });
            // 弹出键盘内部包含一个KeyboardView，在此绑定输入两者关联。
            mPopupKeyboard.attach(mInputViewEditCarItem, mActivity);
            //设置默认是【河北省】
            mPopupKeyboard.getKeyboardEngine().setLocalProvinceName(mActivity.getResources().getString(R.string.hebei_province));

            //绑定完毕软键盘立即传递给Activity
            if (myArchiveFileCarInfoAddListener != null) {
                if (_fileCarInfoAddSubmit != null) {
                    myArchiveFileCarInfoAddListener.sendCarLicenseNumberPopupKeyboard(identifierNumber, _fileCarInfoAddSubmit, mPopupKeyboard);
                } else {
                    ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                }
            }

            //输入变更回调接口
            mPopupKeyboard.getController().addOnInputChangedListener(new OnInputChangedListener() {
                @Override
                public void onChanged(String number, boolean isCompleted) {

                    //监听变更为新能源
                    LogUtils.d(ConstantSign.LOGCAT_NOMAL + "打印输入的车牌号码-->" + number);

//                  mPopupKeyboard.getController().updateNumberLockType(number,true);

                    //这里直接传递消息给外侧界面

                    if (!TextUtils.isEmpty(number)) {

                        if (myArchiveFileCarInfoAddListener != null) {
                            if (_fileCarInfoAddSubmit != null) {
                                myArchiveFileCarInfoAddListener.inputCarLicenseNumber(identifierNumber, number, _fileCarInfoAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车牌号 文本为空");
                    }

                    if (isCompleted) {
//                        method_check_licenseNumber();
                        mPopupKeyboard.dismiss(mActivity);

                    } else {
                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "键盘的变动--不是完成状态");
                    }


                }

                @Override
                public void onCompleted(String number, boolean isAutoCompleted) {
                    mPopupKeyboard.dismiss(mActivity);
                }
            });


            radioGroupCarType_carItem.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                    //是普通车
                    if (checkedId == R.id.radiobuttoncarnomal_caritem) {


                        try {
                            //do

                            radioButtonCarNomal_carItem.setChecked(true);
                            radioButtonCarNomal_carItem.setTextColor(ColorUtils.getColor(R.color.headmanblue));

                            radioButtonCarNewEnergy_carItem.setChecked(false);
                            radioButtonCarNewEnergy_carItem.setTextColor(ColorUtils.getColor(R.color.dark));

                            //如果用户都填写了车牌号，刚好是8位，那么删除掉最后一位
                            int length = mInputViewEditCarItem.getNumber().length();

                            if (length > CarLicenseNumberType.LICENSE_NUMBERTYPE_7.getLength()) {
                                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "是新能源-需要删除掉最后一个");
                                //删除掉最后一个
                                mInputViewEditCarItem.removeLastCharOfNumber();
                            }

                            //do
                            if (mPopupKeyboard.getController() != null) {
                                //重新更新下数字
                                mPopupKeyboard.getController().setSwitchVerify(true).updateNumber(mInputViewEditCarItem.getNumber(), InputViewSelectKeyboard.KEYBOARD_SHOW_YES.getIndex());
                            }

                        } catch (Exception e) {
                            //catch
                            e.printStackTrace();
                        }


                    } else if (checkedId == R.id.radiobuttoncarnewenergy_caritem) {
                        //是新能源车

                        try {
                            //do
                            radioButtonCarNomal_carItem.setChecked(false);
                            radioButtonCarNomal_carItem.setTextColor(ColorUtils.getColor(R.color.dark));

                            radioButtonCarNewEnergy_carItem.setChecked(true);
                            radioButtonCarNewEnergy_carItem.setTextColor(ColorUtils.getColor(R.color.headmanblue));

                            //do
                            //锁定是新能源
                            if (mPopupKeyboard.getController() != null) {

                                mPopupKeyboard.getController()//
                                        .setDebugEnabled(true)//
                                        .setSwitchVerify(true)
                                        .updateNumberLockType(mInputViewEditCarItem.getNumber(), true, InputViewSelectKeyboard.KEYBOARD_SHOW_YES.getIndex());//

                            }

                        } catch (Exception e) {
                            //catch
                            e.printStackTrace();
                        }


                    }

                }
            });


            //-----------------------------------------------------------------------
            //如果是第一条数据，那么隐藏叉号.
            int indexPosition = getItemPosition(_fileCarInfoAddSubmit);

            if (indexPosition == 0) {
                imageViewDeleteInFileCarInfo.setVisibility(View.GONE);
            } else {
                imageViewDeleteInFileCarInfo.setVisibility(View.VISIBLE);
            }
            //-----------------------------------------------------------------------

            //绑定车辆车牌
//            if (editViewCarCard != null) {
//                VehicleKeyboardHelper.bind(editViewCarCard);
//            }

            //发动机号
            ClearEditTextTouch editViewCarMotor = (ClearEditTextTouch) baseViewHolder.getView(R.id.editviewcarmotorcar);

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //对车辆品牌的选中的弹窗监听
            linearLayoutCarBrandNameOut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    LogUtils.d(ConstantSign.LOGCAT_NOMAL + "linearLayoutCarBrandNameOut--添加车辆品牌的监听");

                    //弹品牌的窗，和接口对应；
                    if (myArchiveFileCarInfoAddListener != null) {
                        myArchiveFileCarInfoAddListener.chooseCarBranchType(identifierNumber, _fileCarInfoAddSubmit);
                    }

                }
            });

            textVieweBrandModel.setText(carBranchName);

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //对车系的弹窗监听
            layoutCarModelSecondSide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    LogUtils.d(ConstantSign.LOGCAT_NOMAL + "linearLayoutCarBrandNameOut--添加车辆车系的监听");

                    if (myArchiveFileCarInfoAddListener != null) {
                        myArchiveFileCarInfoAddListener.chooseCarSeries(identifierNumber, _fileCarInfoAddSubmit);
                    }

                }
            });

            textViewCarModelSecond.setText(carSeriesName);

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //对于手动录入的监听
            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //签约人的监听器
            TextWatcher inputCarModelTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable seditable) {
                    String input = seditable.toString();

                    if (!TextUtils.isEmpty(input)) {

                        if (myArchiveFileCarInfoAddListener != null) {
                            if (_fileCarInfoAddSubmit != null) {
                                myArchiveFileCarInfoAddListener.inputCarModel(identifierNumber, input, _fileCarInfoAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车辆车型的 文本为空");
                    }
                }
            };

            if ((editViewInputEvaluateCarModel.getTag() != null) && (editViewInputEvaluateCarModel.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewInputEvaluateCarModel.removeTextChangedListener((TextWatcher) editViewInputEvaluateCarModel.getTag());
            }

            //移除之后设置名称
            editViewInputEvaluateCarModel.setText(carModelName);
            //添加监听
            editViewInputEvaluateCarModel.addTextChangedListener(inputCarModelTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewInputEvaluateCarModel.setTag(inputCarModelTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //签约人的监听器
            TextWatcher inputCarVinTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable seditable) {

                    String input = seditable.toString();

                    if (!TextUtils.isEmpty(input)) {

                        if (myArchiveFileCarInfoAddListener != null) {
                            if (_fileCarInfoAddSubmit != null) {
                                myArchiveFileCarInfoAddListener.inputCarVin(identifierNumber, input, _fileCarInfoAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车架号的 文本为空");
                    }
                }
            };

            if ((editViewCarFrame.getTag() != null) && (editViewCarFrame.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewCarFrame.removeTextChangedListener((TextWatcher) editViewCarFrame.getTag());
            }

            //移除之后设置名称
            editViewCarFrame.setText(carVin);
            //添加监听
            editViewCarFrame.addTextChangedListener(inputCarVinTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewCarFrame.setTag(inputCarVinTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //发动机号
            TextWatcher inputCarEngineNumTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable seditable) {

                    String input = seditable.toString();

                    if (!TextUtils.isEmpty(input)) {

                        if (myArchiveFileCarInfoAddListener != null) {
                            if (_fileCarInfoAddSubmit != null) {
                                myArchiveFileCarInfoAddListener.inputCarEngineNum(identifierNumber, input, _fileCarInfoAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车架号的 文本为空");
                    }
                }
            };

            if ((editViewCarMotor.getTag() != null) && (editViewCarMotor.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewCarMotor.removeTextChangedListener((TextWatcher) editViewCarMotor.getTag());
            }

            //移除之后设置名称
            editViewCarMotor.setText(engineNum);
            //添加监听
            editViewCarMotor.addTextChangedListener(inputCarEngineNumTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewCarMotor.setTag(inputCarEngineNumTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //车牌号监听器
//            TextWatcher inputCarLicenseNumberTextWatcher = new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                }
//
//                @Override
//                public void afterTextChanged(Editable seditable) {
//
//                    String input = seditable.toString();
//
//                    if (!TextUtils.isEmpty(input)) {
//
//                        if (myArchiveFileCarInfoAddListener != null) {
//                            if (_fileCarInfoAddSubmit != null) {
//                                myArchiveFileCarInfoAddListener.inputCarLicenseNumber(identifierNumber, input, _fileCarInfoAddSubmit);
//                            } else {
//                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
//                            }
//                        }
//
//                    } else {
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车牌号 文本为空");
//                    }
//                }
//            };
//
//            if ((editViewCarCard.getTag() != null) && (editViewCarCard.getTag() instanceof TextWatcher)) {
//                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
//                editViewCarCard.removeTextChangedListener((TextWatcher) editViewCarCard.getTag());
//            }
//
//            //移除之后设置名称
//            editViewCarCard.setText(licenseNumber);
//            //添加监听
//            editViewCarCard.addTextChangedListener(inputCarLicenseNumberTextWatcher);
//            //设置标记,为了上面删除监听使用
//            editViewCarCard.setTag(inputCarLicenseNumberTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

//            //车牌号需要一个监听
//            //输入完毕校验车牌的正确性，如果是这个正确的输入完毕就校验一次【车牌是否是在黑名单】
//            RxTextView.afterTextChangeEvents(editViewCarCard)
//                    .debounce(ConstantApi.TIME_DELAY500, TimeUnit.MILLISECONDS)
//                    .skip(ConstantApi.SKIP_CAR_NUMBER)//过滤掉前4个都行 【京Q58A77】
//                    .observeOn(AndroidSchedulers.mainThread())//
//                    .subscribeOn(AndroidSchedulers.mainThread())//
//                    .subscribe(new Consumer<TextViewAfterTextChangeEvent>() {
//                        @Override
//                        public void accept(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Throwable {
//
//                            try {
//
//                                //do
//                                if (textViewAfterTextChangeEvent != null) {
//
//                                    String editResult = textViewAfterTextChangeEvent.getView().getText().toString();
//                                    //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "输入完毕的数据是-->" + editResult);
//
//                                    //如果小区id不是空，并且车牌也不是空；  && !TextUtils.isEmpty(upload_CommunityId)
//                                    if (!TextUtils.isEmpty(editResult)) {
//
//                                        //校验【不是空】说明输入的是正确的车牌，那么校验【该完整车牌是否在黑名单之中】
//                                        //校验如果是空，说明输入的是错误的车牌
//                                        if (VehiclePlateNoUtil.getVehiclePlateNo(editViewCarCard.getText().toString().trim()) == null) {
//
//                                            LogUtils.d(ConstantSign.LOGCAT_NOMAL + "手机校验是正确的车牌");
//
//                                            String result = getContext().getResources().getString(R.string.please_input_carnumersure);
//
//                                            ToastUtils.showShort(result);
//
//                                            //PopTip.show(result).iconWarning();
//                                            //PopTip.show(result).setAlign(DialogXStyle.PopTipSettings.ALIGN.CENTER);
////                                            MessageDialog.show("提示", result, "确定")
////                                                    .setTitleIcon(R.mipmap.ic_launcher)
////                                                    .setOkButton(new OnDialogButtonClickListener<MessageDialog>() {
////                                                        @Override
////                                                        public boolean onClick(MessageDialog baseDialog, View v) {
////                                                            //PopTip.show("点击确定按钮");
////                                                            return false;
////                                                        }
////                                                    });
//
//                                        }
//
//                                    }
//                                }
//
//                            } catch (Exception e) {
//                                //catch
//                                if (e != null) {
//
//                                }
//                            }
//                        }
//                    });

            //imageViewDeleteInFileCarInfo-----------------------------------
            if (imageViewDeleteInFileCarInfo != null) {
                imageViewDeleteInFileCarInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (myArchiveFileCarInfoAddListener != null) {
                            if (_fileCarInfoAddSubmit != null) {
                                myArchiveFileCarInfoAddListener.deleteCarInfo(identifierNumber, _fileCarInfoAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    }
                });
            }

            //-----------------------------------------------------------------------------------------------------------------------------------------------


        }

    }
}
